package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.activity.AppPictureActivity;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilBitmap;

import longcaisuyun.longcai.com.net.GetCityList;



public class JiaMengPhotoActivity extends AppPictureActivity implements View.OnClickListener {

    private Dialog dialog;
    ImageView icon_1, icon_2, icon_3, icon_4, icon_5;
    String photo[] = new String[5];
    int count = 0;
    String username = "";
    //身份证
    String cardnumber = "";
    //城市id
    String cityid = "";
    //邀请人手机号码
    String mobile2 = "";
    //车型id
    String cid = "";
    //车牌号
    String license = "";
    //电话号
    String mobile = "";
    Button buttondone;
    //是否能点下一步
    public static boolean next=true;
    public static String id;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jia_meng_photo);
        MyApplication.getInstance().addActivity(this);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
        Intent intent = getIntent();
        username = intent.getStringExtra("username");
        cardnumber = intent.getStringExtra("cardnumber");
        cityid = intent.getStringExtra("cityid");
        mobile2 = intent.getStringExtra("mobile2");
        cid = intent.getStringExtra("cid");
        license = intent.getStringExtra("license");
        mobile = intent.getStringExtra("mobile");
    }

    private void ViewInit() {
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
    fanhui_lin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    icon_1 = (ImageView) findViewById(R.id.icon_1);
    icon_1.setOnClickListener(this);
    icon_2 = (ImageView) findViewById(R.id.icon_2);
    icon_2.setOnClickListener(this);
    icon_3 = (ImageView) findViewById(R.id.icon_3);
    icon_3.setOnClickListener(this);
    icon_4 = (ImageView) findViewById(R.id.icon_4);
    icon_4.setOnClickListener(this);
    icon_5 = (ImageView) findViewById(R.id.icon_5);
    icon_5.setOnClickListener(this);
    buttondone = (Button) findViewById(R.id.buttondone);
    buttondone.setOnClickListener(this);
}

    /**
     * @param v
     */
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.icon_1:
                dialg(icon_1);
                count = 0;
                break;
            case R.id.icon_2:
                dialg(icon_2);
                count = 1;
                break;

            case R.id.icon_3:
                dialg(icon_3);
                count = 2;
                break;

            case R.id.icon_4:
                dialg(icon_4);
                count = 3;
                break;

            case R.id.icon_5:
                dialg(icon_5);
                count = 4;
                break;
            case R.id.buttondone:
                if (photo[0]==null||photo[1]==null||photo[2]==null||photo[3]==null||photo[4]==null){
                    Toast.makeText(JiaMengPhotoActivity.this, "您需要上传全部证件照片", Toast.LENGTH_SHORT).show();
                }else{
//                    注册接口
//                    final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
//                    final FinalHttp finalHttp = new FinalHttp();
//                    AjaxParams params = new AjaxParams();
//                    JSONObject jsonObject = new JSONObject();
//                    try {
//                        jsonObject.put("mobile", username);
//                        jsonObject.put("cardnumber", cardnumber);
//                        jsonObject.put("cityid", cityid);
//                        jsonObject.put("mobile", mobile);
//                        jsonObject.put("cid", cid);
//                        jsonObject.put("license", license);
//                        jsonObject.put("mobile2", mobile2);
//                        jsonObject.put("picurl_id", photo[0]);
//                        jsonObject.put("picurl_driver", photo[1]);
//                        jsonObject.put("picurl_car", photo[2]);
//                        jsonObject.put("picurl_license", photo[3]);
//                        jsonObject.put("picurl_use", photo[4]);
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                    params.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
//                    finalHttp.post(publicUrl.zhuce, params, new AjaxCallBack<String>() {
//                        public void onStart() {
//                            super.onStart();
//                            MyApplication.myviewutil.startProgressDialog(JiaMengPhotoActivity.this, finalHttp, publicUrl.mobilecode);
//                        }
//
//                        public void onSuccess(String s) {
//                            super.onSuccess(s);
//                            try {
//                                JSONObject jsonObject = new JSONObject(secretAESDESede.decrypt(s));
//                                Log.e("回来什么了", jsonObject.toString());
//                                String Message = jsonObject.getString("message");
//                                String driverid = jsonObject.getString("driverid");
//                                if (jsonObject.getString("message").equals("1")) {
//                                    Toast.makeText(JiaMengPhotoActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
//                                    finish();
//                                }
//                                if (jsonObject.getString("message").equals("2")) {
//                                    Toast.makeText(JiaMengPhotoActivity.this, "您的手机号已经注册，请直接登录", Toast.LENGTH_SHORT).show();
//                                }
//                                if (jsonObject.getString("message").equals("0")) {
//                                    Toast.makeText(JiaMengPhotoActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
//                                }
//                                if (jsonObject.getString("message").equals("3")){
//                                    Toast.makeText(JiaMengPhotoActivity.this, "图片错误，请重试", Toast.LENGTH_SHORT).show();
//                                }
//                            } catch (JSONException e) {
//                                e.printStackTrace();
//                            }
//                            MyApplication.myviewutil.stopProgressDialog();
//                        }
//
//                        public void onFailure(Throwable t, int errorNo, String strMsg) {
//                            super.onFailure(t, errorNo, strMsg);
//                            Log.e("回来什么了", "111111");
//                            Toast.makeText(JiaMengPhotoActivity.this, "网络故障,请稍后重试", Toast.LENGTH_SHORT).show();
//                            MyApplication.myviewutil.stopProgressDialog();
//                        }
//                    });
                    if (next){
                        MyApplication.myviewutil.startProgressDialog(JiaMengPhotoActivity.this);
                        new GetCityList(mobile,username, cardnumber, cityid, mobile2, cid, license,  photo[0], photo[1], photo[2], photo[3], photo[4], new AsyCallBack<GetCityList.Info>() {
                            @Override
                            public void onSuccess(String toast, int type, GetCityList.Info info) throws Exception {
                                super.onSuccess(toast, type, info);
                                MyApplication.myviewutil.stopProgressDialog();
                                switch (info.message){
                                    case "0":
                                        Toast.makeText(JiaMengPhotoActivity.this, "资料上传失败", Toast.LENGTH_SHORT).show();
                                        break;
                                    case "1":
                                        id=info.id;
//                                    Toast.makeText(JiaMengPhotoActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                                        Intent intent=new Intent(JiaMengPhotoActivity.this,DaTiActivity.class);
                                        intent.putExtra("driverid",info.id);
                                        startActivity(intent);
                                        break;
                                    case "2":
                                        Toast.makeText(JiaMengPhotoActivity.this,"您已提交申请，请耐心等待审核",Toast.LENGTH_SHORT).show();
                                        break;
                                    case "3":
                                        Toast.makeText(JiaMengPhotoActivity.this,"资料上传失败",Toast.LENGTH_SHORT).show();
                                        break;
                                }

                            }

                            @Override
                            public void onFail(String toast, int type) throws Exception {
                                super.onFail(toast, type);
                                MyApplication.myviewutil.stopProgressDialog();
                                Toast.makeText(JiaMengPhotoActivity.this, "" + "失败", Toast.LENGTH_SHORT).show();
                            }
                        }).execute(JiaMengPhotoActivity.this);
                    }else{
                        Intent intent=new Intent(JiaMengPhotoActivity.this,DaTiActivity.class);
                        intent.putExtra("driverid",id);
                        startActivity(intent);
                    }

                }

                break;
        }
    }

    protected String getCameraAbsolutePath() {
        return Environment.getExternalStorageDirectory() + "/168suyun";
    }
    protected void resultPhotoPath(ImageView imageView, String s) {
        imageView.setImageBitmap(BitmapFactory.decodeFile(s));
        try {
            photo[count] = UtilBitmap.toBase64ByPath(s, 720, 1280, 50);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dialg(final ImageView Iv) {
        LayoutInflater inflater = LayoutInflater.from(JiaMengPhotoActivity.this);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.item_photo_icon, null);

        dialog = new AlertDialog.Builder(JiaMengPhotoActivity.this).create();
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.mypopwindow_anim_style);
        dialog.show();
        WindowManager windowManager = JiaMengPhotoActivity.this.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); // 设置宽度
        dialog.getWindow().setAttributes(lp);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog.setContentView(layout);
        TextView t_carame = (TextView) layout.findViewById(R.id.t_camera);
        TextView t_xiangce = (TextView) layout.findViewById(R.id.t_xiangce);
        TextView t_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        t_carame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JiaMengPhotoActivity.this.startCamera(Iv);
                dialog.dismiss();
            }
        });
        t_xiangce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JiaMengPhotoActivity.this.startAlbum(Iv);
                dialog.dismiss();
            }
        });
        t_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    protected Crop getCrop() {

        return new Crop().setCrop(true);

    }

    public void onBackPressed() {
        super.onBackPressed();
    }


}
